package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartSerice;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartSericeImpl implements ShoppingCartSerice {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private DishMapper dishMapper;
@Autowired
private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //1、传入用户ID查询购物车内
                List<ShoppingCart> shoppingCartListlist = shoppingCartMapper.list(shoppingCart);

                log.info("查看购物车:{}",shoppingCartListlist);
                if (shoppingCartListlist != null && shoppingCartListlist.size() ==1){
                    ShoppingCart shoppingCart1 = shoppingCartListlist.get(0);
                    Integer number = shoppingCart1.getNumber();
                    shoppingCart1.setNumber(number+1);
                    shoppingCartMapper.update(shoppingCart1);
                }else {
                    if (shoppingCart.getDishId() !=null){
                        Dish dish = dishMapper.getById(shoppingCart.getDishId());
                        shoppingCart.setImage(dish.getImage());
                        shoppingCart.setAmount(dish.getPrice());
                        shoppingCart.setName(dish.getName());
                    }else {
                        SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                        shoppingCart.setAmount(setmealVO.getPrice());
                        shoppingCart.setImage(setmealVO.getImage());
                        shoppingCart.setName(setmealVO.getName());
                    }
                    shoppingCart.setNumber(1);
                    shoppingCart.setCreateTime(LocalDateTime.now());
                    shoppingCartMapper.insert(shoppingCart);
                }

    }

    @Override
    public List<ShoppingCart> getAll() {
        Long userId = BaseContext.getCurrentId();


        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }


    /**
     * 清除所有
     */
    @Override
    public void clean() {
        //根据用户ID删除购物车中所有
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }


    /**
     * 删除购物车中的某一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //1、查询确定对应菜品或者套餐
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //2、判断数量大于1则将数量减一，等于1则直接删除
        ShoppingCart shoppingCart1 = shoppingCartList.get(0);
        if (shoppingCart1.getNumber()>1){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.update(shoppingCart1);
        }else {shoppingCartMapper.delete(shoppingCart1);}

    }


}
